/**
 * 结构型设计模式Java示例代码
 */
const structuralPatternCodes = {
  'Adapter': `/**
 * 适配器模式示例
 */
// 目标接口
interface Target {
    void request();
}

// 被适配者
class Adaptee {
    public void specificRequest() {
        System.out.println("Adaptee's specific request");
    }
}

// 类适配器 (通过继承)
class ClassAdapter extends Adaptee implements Target {
    @Override
    public void request() {
        System.out.println("ClassAdapter: Converting request to specificRequest");
        specificRequest();
    }
}

// 对象适配器 (通过组合)
class ObjectAdapter implements Target {
    private Adaptee adaptee;
    
    public ObjectAdapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }
    
    @Override
    public void request() {
        System.out.println("ObjectAdapter: Converting request to specificRequest");
        adaptee.specificRequest();
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        // 使用类适配器
        Target targetWithClassAdapter = new ClassAdapter();
        targetWithClassAdapter.request();
        
        System.out.println("------------------------");
        
        // 使用对象适配器
        Adaptee adaptee = new Adaptee();
        Target targetWithObjectAdapter = new ObjectAdapter(adaptee);
        targetWithObjectAdapter.request();
    }
}`,

  'Bridge': `/**
 * 桥接模式示例
 */
// 实现部分接口
interface Implementor {
    void operationImpl();
}

// 具体实现A
class ConcreteImplementorA implements Implementor {
    @Override
    public void operationImpl() {
        System.out.println("ConcreteImplementorA: Operation implementation");
    }
}

// 具体实现B
class ConcreteImplementorB implements Implementor {
    @Override
    public void operationImpl() {
        System.out.println("ConcreteImplementorB: Operation implementation");
    }
}

// 抽象部分
abstract class Abstraction {
    protected Implementor implementor;
    
    public Abstraction(Implementor implementor) {
        this.implementor = implementor;
    }
    
    public void setImplementor(Implementor implementor) {
        this.implementor = implementor;
    }
    
    public abstract void operation();
}

// 扩展抽象部分
class RefinedAbstraction extends Abstraction {
    public RefinedAbstraction(Implementor implementor) {
        super(implementor);
    }
    
    @Override
    public void operation() {
        System.out.println("RefinedAbstraction: Refined operation");
        implementor.operationImpl();
    }
    
    public void additionalOperation() {
        System.out.println("RefinedAbstraction: Additional operation");
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        // 创建具体实现
        Implementor implementorA = new ConcreteImplementorA();
        Implementor implementorB = new ConcreteImplementorB();
        
        // 创建抽象部分并关联实现
        Abstraction abstraction = new RefinedAbstraction(implementorA);
        abstraction.operation();
        
        System.out.println("------------------------");
        
        // 切换实现
        abstraction.setImplementor(implementorB);
        abstraction.operation();
        
        System.out.println("------------------------");
        
        // 使用扩展方法
        RefinedAbstraction refinedAbstraction = (RefinedAbstraction) abstraction;
        refinedAbstraction.additionalOperation();
    }
}`,

  'Composite': `/**
 * 组合模式示例
 */
import java.util.ArrayList;
import java.util.List;

// 组件抽象类
abstract class Component {
    protected String name;
    
    public Component(String name) {
        this.name = name;
    }
    
    public abstract void operation();
    public abstract void add(Component component);
    public abstract void remove(Component component);
    public abstract Component getChild(int index);
    public abstract void display(int depth);
}

// 叶子节点
class Leaf extends Component {
    public Leaf(String name) {
        super(name);
    }
    
    @Override
    public void operation() {
        System.out.println("Leaf " + name + " operation");
    }
    
    @Override
    public void add(Component component) {
        System.out.println("Cannot add to a leaf");
    }
    
    @Override
    public void remove(Component component) {
        System.out.println("Cannot remove from a leaf");
    }
    
    @Override
    public Component getChild(int index) {
        System.out.println("Cannot get child from a leaf");
        return null;
    }
    
    @Override
    public void display(int depth) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append('-');
        }
        sb.append(name);
        System.out.println(sb.toString());
    }
}

// 复合节点
class Composite extends Component {
    private List<Component> children = new ArrayList<>();
    
    public Composite(String name) {
        super(name);
    }
    
    @Override
    public void operation() {
        System.out.println("Composite " + name + " operation");
        
        // 对所有子节点执行操作
        for (Component component : children) {
            component.operation();
        }
    }
    
    @Override
    public void add(Component component) {
        children.add(component);
    }
    
    @Override
    public void remove(Component component) {
        children.remove(component);
    }
    
    @Override
    public Component getChild(int index) {
        if (index >= 0 && index < children.size()) {
            return children.get(index);
        }
        return null;
    }
    
    @Override
    public void display(int depth) {
        // 显示当前节点
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append('-');
        }
        sb.append(name);
        System.out.println(sb.toString());
        
        // 显示所有子节点
        for (Component component : children) {
            component.display(depth + 2);
        }
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        // 创建根节点
        Composite root = new Composite("Root");
        
        // 创建叶子节点
        Leaf leaf1 = new Leaf("Leaf 1");
        Leaf leaf2 = new Leaf("Leaf 2");
        
        // 创建复合节点
        Composite branch1 = new Composite("Branch 1");
        Composite branch2 = new Composite("Branch 2");
        
        // 构建树结构
        root.add(leaf1);
        root.add(branch1);
        
        branch1.add(leaf2);
        branch1.add(branch2);
        
        // 显示树结构
        System.out.println("Tree structure:");
        root.display(0);
        
        System.out.println("------------------------");
        
        // 执行操作
        System.out.println("Executing operations:");
        root.operation();
    }
}`,

  'Decorator': `/**
 * 装饰器模式示例
 */
// 组件接口
interface Component {
    void operation();
}

// 具体组件
class ConcreteComponent implements Component {
    @Override
    public void operation() {
        System.out.println("ConcreteComponent: Basic operation");
    }
}

// 装饰器基类
abstract class Decorator implements Component {
    protected Component component;
    
    public Decorator(Component component) {
        this.component = component;
    }
    
    // 默认将操作委托给被装饰的组件
    @Override
    public void operation() {
        component.operation();
    }
}

// 具体装饰器A
class ConcreteDecoratorA extends Decorator {
    public ConcreteDecoratorA(Component component) {
        super(component);
    }
    
    @Override
    public void operation() {
        super.operation();
        addedBehaviorA();
    }
    
    private void addedBehaviorA() {
        System.out.println("ConcreteDecoratorA: Added behavior A");
    }
}

// 具体装饰器B
class ConcreteDecoratorB extends Decorator {
    public ConcreteDecoratorB(Component component) {
        super(component);
    }
    
    @Override
    public void operation() {
        super.operation();
        addedBehaviorB();
    }
    
    private void addedBehaviorB() {
        System.out.println("ConcreteDecoratorB: Added behavior B");
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        // 创建具体组件
        Component component = new ConcreteComponent();
        System.out.println("Basic component:");
        component.operation();
        
        System.out.println("------------------------");
        
        // 使用装饰器A装饰
        Component decoratedWithA = new ConcreteDecoratorA(component);
        System.out.println("Component decorated with A:");
        decoratedWithA.operation();
        
        System.out.println("------------------------");
        
        // 使用装饰器B装饰
        Component decoratedWithB = new ConcreteDecoratorB(component);
        System.out.println("Component decorated with B:");
        decoratedWithB.operation();
        
        System.out.println("------------------------");
        
        // 组合多个装饰器
        Component decoratedWithAB = new ConcreteDecoratorB(new ConcreteDecoratorA(component));
        System.out.println("Component decorated with A and B:");
        decoratedWithAB.operation();
    }
}`,

  'Facade': `/**
 * 外观模式示例
 */
// 子系统类A
class SubsystemA {
    public void operationA() {
        System.out.println("SubsystemA: Operation A");
    }
}

// 子系统类B
class SubsystemB {
    public void operationB() {
        System.out.println("SubsystemB: Operation B");
    }
}

// 子系统类C
class SubsystemC {
    public void operationC() {
        System.out.println("SubsystemC: Operation C");
    }
}

// 外观类
class Facade {
    private SubsystemA subsystemA;
    private SubsystemB subsystemB;
    private SubsystemC subsystemC;
    
    public Facade() {
        this.subsystemA = new SubsystemA();
        this.subsystemB = new SubsystemB();
        this.subsystemC = new SubsystemC();
    }
    
    // 外观方法1 - 简单操作
    public void operation1() {
        System.out.println("Facade: Operation 1");
        subsystemA.operationA();
        subsystemB.operationB();
    }
    
    // 外观方法2 - 复杂操作
    public void operation2() {
        System.out.println("Facade: Operation 2");
        subsystemB.operationB();
        subsystemC.operationC();
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        // 创建外观
        Facade facade = new Facade();
        
        // 使用简化的接口
        System.out.println("Executing Operation 1 through Facade:");
        facade.operation1();
        
        System.out.println("------------------------");
        
        System.out.println("Executing Operation 2 through Facade:");
        facade.operation2();
        
        System.out.println("------------------------");
        
        // 直接访问子系统 (不推荐，但可能)
        System.out.println("Directly accessing subsystems (not recommended):");
        SubsystemA subsystemA = new SubsystemA();
        subsystemA.operationA();
    }
}`,

  'Flyweight': `/**
 * 享元模式示例
 */
import java.util.HashMap;
import java.util.Map;

// 享元接口
interface Flyweight {
    void operation(String extrinsicState);
}

// 具体享元
class ConcreteFlyweight implements Flyweight {
    private final String intrinsicState;
    
    public ConcreteFlyweight(String intrinsicState) {
        this.intrinsicState = intrinsicState;
        System.out.println("Creating a flyweight with intrinsic state: " + intrinsicState);
    }
    
    @Override
    public void operation(String extrinsicState) {
        System.out.println("ConcreteFlyweight: Intrinsic State = " + intrinsicState 
                + ", Extrinsic State = " + extrinsicState);
    }
}

// 不共享的享元 (一个可选的扩展)
class UnsharedConcreteFlyweight implements Flyweight {
    private final String allState;
    
    public UnsharedConcreteFlyweight(String allState) {
        this.allState = allState;
        System.out.println("Creating an unshared flyweight with state: " + allState);
    }
    
    @Override
    public void operation(String extrinsicState) {
        System.out.println("UnsharedConcreteFlyweight: All State = " + allState 
                + ", Extrinsic (ignored) = " + extrinsicState);
    }
}

// 享元工厂
class FlyweightFactory {
    private Map<String, Flyweight> flyweights = new HashMap<>();
    
    // 获取享元，不存在则创建
    public Flyweight getFlyweight(String key) {
        if (flyweights.containsKey(key)) {
            System.out.println("Reusing existing flyweight with key: " + key);
            return flyweights.get(key);
        } else {
            Flyweight flyweight = new ConcreteFlyweight(key);
            flyweights.put(key, flyweight);
            return flyweight;
        }
    }
    
    public int getFlyweightCount() {
        return flyweights.size();
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        // 创建享元工厂
        FlyweightFactory factory = new FlyweightFactory();
        
        // 使用工厂获取享元，传递外部状态
        Flyweight flyweight1 = factory.getFlyweight("shared-1");
        flyweight1.operation("First Call");
        
        Flyweight flyweight2 = factory.getFlyweight("shared-2");
        flyweight2.operation("Second Call");
        
        // 重用已有享元
        Flyweight flyweight1Again = factory.getFlyweight("shared-1");
        flyweight1Again.operation("Third Call");
        
        // 创建不共享的享元
        Flyweight unsharedFlyweight = new UnsharedConcreteFlyweight("unshared");
        unsharedFlyweight.operation("Fourth Call");
        
        System.out.println("Total shared flyweights: " + factory.getFlyweightCount());
    }
}`,

  'Proxy': `/**
 * 代理模式示例
 */
// 主题接口
interface Subject {
    void request();
}

// 真实主题
class RealSubject implements Subject {
    @Override
    public void request() {
        System.out.println("RealSubject: Handling request");
    }
}

// 代理
class Proxy implements Subject {
    private RealSubject realSubject;
    
    @Override
    public void request() {
        // 懒加载真实主题
        if (realSubject == null) {
            System.out.println("Proxy: Creating and initializing RealSubject");
            realSubject = new RealSubject();
        }
        
        // 前置处理
        preRequest();
        
        // 将请求委托给真实主题
        realSubject.request();
        
        // 后置处理
        postRequest();
    }
    
    private void preRequest() {
        System.out.println("Proxy: Pre-processing request");
    }
    
    private void postRequest() {
        System.out.println("Proxy: Post-processing request");
    }
}

// 保护代理
class ProtectionProxy implements Subject {
    private RealSubject realSubject;
    private String user;
    
    public ProtectionProxy(String user) {
        this.user = user;
        this.realSubject = new RealSubject();
    }
    
    @Override
    public void request() {
        if (checkAccess()) {
            realSubject.request();
        } else {
            System.out.println("ProtectionProxy: Access denied for user " + user);
        }
    }
    
    private boolean checkAccess() {
        System.out.println("ProtectionProxy: Checking access for user " + user);
        return "admin".equals(user);
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        // 使用普通代理
        System.out.println("Using regular proxy:");
        Subject proxy = new Proxy();
        proxy.request();
        
        System.out.println("------------------------");
        
        // 再次调用，观察缓存行为
        System.out.println("Calling proxy again:");
        proxy.request();
        
        System.out.println("------------------------");
        
        // 使用保护代理
        System.out.println("Using protection proxy with regular user:");
        Subject protectionProxy1 = new ProtectionProxy("user");
        protectionProxy1.request();
        
        System.out.println("------------------------");
        
        System.out.println("Using protection proxy with admin user:");
        Subject protectionProxy2 = new ProtectionProxy("admin");
        protectionProxy2.request();
    }
}`
};

module.exports = structuralPatternCodes; 